<?php

namespace App\Http\Service\Baoliao;

use App\Models\Baoliaocollect;
use App\Models\Baoliaopost;
use App\Http\Service\BaseService;
use Illuminate\Support\Facades\DB;
use App\Http\Service\UserService;
class BaoliaocollectService  extends BaseService
{
    private $model;

    public function __construct(Baoliaocollect $model,Baoliaopost $doubanpost,UserService $userService)
    {
        parent::__construct();
        $this->model = $model;
        $this->doubanpost=$doubanpost;
        $this->userService = $userService;
       
    }
    // 爆料收藏列表
    public function getList($request,$data=[])
    {
        $userlist=$this->userService->getList($request);
        $userlist=$userlist['data'];
        //组装数据
        $userdata=[];
        foreach ($userlist as $k => $v) {
            $userdata[$v['id']]=$v['avatar'];
        }



        $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where ='';
        $where.=' where 1=1';
        if(!empty($data['people_id'])){
            $where.=" and B.people_id='{$data['people_id']}' ";
        }
        if(!empty($data['title'])){
            $where.=" and A.title like '%{$data['title']}%' ";
        }

        if(!empty($data['type'])){
            $where.=" and A.type = '{$data['type']}' ";
        }


        //用户列表
        $sql="SELECT A.*,B.people_id as people_id FROM `module_baoliao_post`  as A  INNER JOIN   `module_baoliao_collect` as B ON A.unique_id =B.unique_id {$where} order by B.create_time desc ";
        //die($sql);
        $data=DB::select($sql);
        $data = array_map('get_object_vars', $data);
        foreach($data as &$v){
            $user_id=(int)trim($v['people_id']);
            if(isset($userdata[$user_id])){
                $v['avatar_img'] =$userdata[$user_id];
            }else{
                $v['avatar_img'] ='http://xingfanapp.oss-cn-hangzhou.aliyuncs.com/uploads/modules/2025-06-11/f6dcc8ae-375a-4957-a59c-6aeb6b191b18.jpg';
            }
            //dd($user_id);
            
            $v['save_time'] =uc_time_ago($v['update_time']);
            $v['content'] =json_decode($v['content'],true);       
            $v['purchase_steps'] =json_decode($v['purchase_steps'],true);  
            $v['purchase_screenshots'] =json_decode($v['purchase_screenshots'],true);  
            $v['images'] =json_decode($v['images'],true);
        
            
            
        }
        
        // 计算总数
        $count = count($data);

        $list= array_slice($data, $offset, $limit);
        $total_page=count($list);
        $result=[
            'page'=>$page,
            'limit'=>$limit,
            'count'=>$count,
            'total_page'=>$total_page,
            'data'=>$data,
        ];
        return $result;
    }
   
    /**
     *
     * @param array $params
     * @return array
     */
    public function addDate($request,$data=[])
    {
        $unique_id=$data['unique_id'];
        $people_id=$data['people_id'];
        $info=$this->model->where(['unique_id'=>$unique_id,'people_id'=>$people_id])->first();
        if($info){
            $res=$this->model->where(['unique_id'=>$unique_id,'people_id'=>$people_id])->delete();
            if($res){
                $status='2';
                $msg="取消收藏";
            }else{
                $msg="网络错误,请重试";
                $status='0';
            }
        }else{
            $data['create_time']=date("Y-m-d H:i:s");
            $data['update_time']=date("Y-m-d H:i:s");
            $res=$this->model->insertGetId($data);
            if($res){
                $msg="添加收藏";
                $status='1';
            }else{
                $msg="网络错误,请重试";
                $status='2';
            }
        }
        //更新Post表
        $collect_count=$this->model->where(['unique_id'=>$unique_id])->count();
        $this->doubanpost->where(['unique_id'=>$unique_id])->update(['collect_count'=>$collect_count]);
        if($res){
            $this->success($status);
        }else{
            $this->error();
        }
        
        
    }

     //删除喜欢
    public function del($request,$data=[],$ids)
    {
        $res =  $this->model->whereIn('id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
}
